<?php
/**
 * Created by PhpStorm.
 * User: qq
 * Date: 2018/1/3
 * Time: 9:53
 * 时间任务
 */

namespace Home\Controller;


class TimeTaskController extends CommonController
{

    /**
     * 该文件中所有任务执行频率，默认1天，单位：秒
     * @var int
     */
    const EXE_TIMES = 86400;

    /**
     * 取消的订单15天后删除，单位：天
     * @var int
     */
    const ORDER_AUTO_CANCEL_DAY = 15;

    /**
     * 取消的订单15天后删除，单位：天
     * @var int
     */
    const ORDER_EXPRESS_DAY = 2;


    /**
     * 默认方法
     */
    public function index()
    {

        //待付款的订单24小时候后取消订单
        $this->order_timeout_cancel();

        //取消的订单15天后删除
        $this->order_timeout_del();

        //根据快递签收了来确认收货：2天后
        $this->timeout_confirm_receipt();


    }


    /**
     * 待付款的订单24小时候后取消订单
     */
    private function order_timeout_cancel()
    {
        $_break = false;
        $order_model = D('ApiOrder');
        $condition = array();
        $condition['order_status_id'] = '-2';
        $condition['date_added'] = array('lt', time() + self::EXE_TIMES);

        //分批，每批处理100个订单，最多处理5W个订单
        for ($i = 0; $i < 500; $i++) {
            if ($_break) {
                break;
            }
            //查询取消的订单
            $finds = 'order_id,goods_id,quantity,order_num_alias';
            $order_list = $order_model->getOrderList($condition, $finds, '', 100);

            if (empty($order_list)) break;

            foreach ($order_list as $v) {
                $where = array(
                    'order_id' => $v['order_id'],
                );

                $update = array(
                    'order_status_id' => '-1',
                );
                $re = $order_model->editOrder($where, $update);
                if ($re) {
                    M()->execute("UPDATE " . C('DB_PREFIX') . "goods SET quantity = (quantity + " . (int)$v['quantity'] . ") WHERE goods_id = '" . $v['goods_id'] . "' AND subtract = '1'");
                    storage_user_action('-1', '时间任务', '时间任务', '待付款的订单24小时候后取消的订单： ' . $v['order_num_alias']);
                } else {
                    storage_user_action('-1', '时间任务', '时间任务', '待付款的订单24小时候后取消的订单： ' . $v['order_num_alias'] . ' 取消失败');
                    $_break = true;
                    break;
                }
            }
        }

    }


    /**
     * 取消的订单15天后删除
     */
    private function order_timeout_del()
    {
        $_break = false;
        $order_model = D('ApiOrder');
        $condition = array();
        $condition['order_status_id'] = '-1';
        $condition['date_added'] = array('lt', time() - self::ORDER_AUTO_CANCEL_DAY * self::EXE_TIMES);

        //分批，每批处理100个订单，最多处理5W个订单
        for ($i = 0; $i < 500; $i++) {
            if ($_break) {
                break;
            }
            //查询取消的订单
            $finds = 'order_id,order_num_alias';
            $order_list = $order_model->getOrderList($condition, $finds, '', 100);

            if (empty($order_list)) break;

            foreach ($order_list as $v) {

                $where = array(
                    'order_id' => $v['order_id'],
                );

                $re = $order_model->delOrder($where);
                if ($re) {
                    storage_user_action('-1', '时间任务', '时间任务', '取消的订单15天后删除的订单： ' . $v['order_num_alias']);
                } else {
                    storage_user_action('-1', '时间任务', '时间任务', '取消的订单15天后删除的订单： ' . $v['order_num_alias'] . ' 删除失败');
                    $_break = true;
                    break;
                }
            }
        }

    }


    /**
     * 签收的订单，2天后自动确认收货
     */
    private function timeout_confirm_receipt()
    {
        $_break = false;
        $order_model = D('ApiOrder');
        $condition = array();
        $condition['order_status_id'] = '5';

        //分批，每批处理100个订单，最多处理5W个订单
        for ($i = 0; $i < 500; $i++) {
            if ($_break) {
                break;
            }
            //查询取消的订单
            $finds = 'order_id,goods_id,quantity,order_num_alias,express_id,code';
            $order_list = $order_model->getOrderList($condition, $finds, '', 100);
            if (empty($order_list)) break;

            foreach ($order_list as $v) {

                $order_data['code'] = $v['code'];
                $order_data['express_id'] = $v['express_id'];
                //查询快递信息
                $data = getExpress($order_data);

                //等于3,：已经签收了
                if ($data['state'] == '3') {
                    $time = strtotime($data['data'][0]['time']);

                    //超过了两天
                    $data_time = time() - $time;
                    //  172800  两天的秒数
                    if ($data_time > 172800) {

                        //自动确认收货
                        $where = array(
                            'order_id' => $v['order_id'],
                        );

                        $update = array(
                            'order_status_id' => '6',
                        );
                        $re = $order_model->editOrder($where, $update);
                        if ($re) {
                            storage_user_action('-1', '时间任务', '时间任务', '签收的订单超过2天后自动确认收货的订单： ' . $v['order_num_alias']);
                        } else {
                            storage_user_action('-1', '时间任务', '时间任务', '签收的订单超过2天后自动确认收货的订单： ' . $v['order_num_alias'] . ' 取消失败');
                            $_break = true;
                            break;
                        }


                    }

                }


            }
        }

    }


}